package com.thoughtworks.turtlemock.filemover;

import org.jmock.Mock;
import org.jmock.MockObjectTestCase;

import java.io.File;

public class FileMoverJMockTest extends MockObjectTestCase {
    private static final String A_FILE_NAME = "aFileName.ext";
    private static final String A_FILE_PATH = "aFileDir" + File.separator + A_FILE_NAME;
    private static final String A_DIR = "aDir";
    private static final String ANOTHER_FILE_NAME = "anotherFileName.ext";
    private static final String ANOTHER_FILE_PATH = "anotherFileDir" + File.separator + ANOTHER_FILE_NAME;

    private FileAdaptee aFile;
    private FileMover aFileMover;
    private Mock destFileMock;
    private Mock aFileMock;
    private Mock aDirMock;

    protected void setUp() throws Exception {
        super.setUp();
        aDirMock = mock(FileAdaptee.class);
        aDirMock.expects(once()).method("getAbsolutePath").will(returnValue(A_DIR));

        aFileMock = mock(FileAdaptee.class);
        destFileMock = mock(FileAdaptee.class);

        aFileMock.expects(once()).method("getName").will(returnValue(A_FILE_NAME));
        aFileMock.expects(once()).method("moveTo").with(eq(A_DIR + File.separator + A_FILE_NAME)).will(returnValue(destFileMock.proxy()));

        aFile = (FileAdaptee) aFileMock.proxy();
        aFileMover = new FileMover((FileAdaptee) aDirMock.proxy());
    }

    public void testShouldMoveFileToTargetDir() throws Exception {
        aFileMover.move(aFile);
    }

    public void testShouldBeRollbackable() throws Exception {
        aFileMover.move(aFile);
        destFileMock.expects(once()).method("moveTo").with(eq(A_FILE_PATH)).will(returnValue(aFileMock.proxy()));
        aFileMock.expects(once()).method("getAbsolutePath").will(returnValue(A_FILE_PATH));
        aFileMover.rollback();
    }

    public void testShouldRollbackAllMovingActions() throws Exception {
        Mock anotherFileMock = mock(FileAdaptee.class);
        Mock anotherDestFileMock = mock(FileAdaptee.class);
        anotherFileMock.expects(once()).method("getName").will(returnValue(ANOTHER_FILE_NAME));
        anotherFileMock.expects(once()).method("moveTo").with(eq(A_DIR + File.separator + ANOTHER_FILE_NAME)).will(returnValue(anotherDestFileMock.proxy()));
        aDirMock.expects(once()).method("getAbsolutePath").will(returnValue(A_DIR));

        aFileMover.move(aFile);
        aFileMover.move((FileAdaptee) anotherFileMock.proxy());

        destFileMock.expects(once()).method("moveTo").with(eq(A_FILE_PATH)).will(returnValue(aFileMock.proxy()));
        aFileMock.expects(once()).method("getAbsolutePath").will(returnValue(A_FILE_PATH));
        anotherFileMock.expects(once()).method("getAbsolutePath").will(returnValue(ANOTHER_FILE_PATH));
        anotherDestFileMock.expects(once()).method("moveTo").with(eq(ANOTHER_FILE_PATH));

        aFileMover.rollback();
    }
}
